package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppSystemService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AppNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 消息  --黑哲
 */
@Controller
@RequestMapping("/app/new")
public class AppNewController extends ClientBaseController {

    @Autowired
    AppNewService appNewService;

    @Autowired
    AppUserService appUserService;

    @Autowired
    AppSystemService appSystemService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 添加会话
     * @param dfuseridid  对方的用户id
     * @return
     */
    @RequestMapping("/addConversation")
    @ResponseBody
    public Result addConversation(Integer dfuseridid) {
        Integer userId = (Integer)redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        AppUser user =appUserService.get(userId);
        if (user ==null) {
            return errorJSON("用户不能为空");
        }
        if (StringUtils.isEmpty(dfuseridid)) {//接受着
            return errorJSON("接受着不能为空");
        }
        Integer integer = appNewService.addConversation(user.getId(), dfuseridid);
        if (integer == 1) {
            return errorJSON("添加失败");
        }
        return sucessJSON("添加成功");
    }
    /**
     * 删除会话
     * @param id  获取会话的主键id
     * @return
     */
    @RequestMapping("/delConversation")
    @ResponseBody
    public Result delConversation(Integer id) {
        if (StringUtils.isEmpty(id)) {
            return errorJSON("id不能为空");
        }
        appNewService.delConversation(id);
        return sucessJSON("删除成功");
    }

    /**
     * 获取会话
     * @param pageno  第几页
     * @return
     * @throws Exception
     */
    @RequestMapping("/getConversation")
    @ResponseBody
    public Result getConversation(Integer pageno) throws Exception {
        Integer userId = (Integer)redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        AppUser user =appUserService.get(userId);
        if (user ==null) {
            return errorJSON("用户不能为空");
        }
        if (StringUtils.isEmpty(pageno)) {//第几页
            return errorJSON("pageno不能为空");
        }
        return sucessJSON(appNewService.getConversation(user.getId(),pageno));
    }


}
